package com.example.code_school.controller;

import com.example.code_school.entity.Elective;
import com.example.code_school.entity.Grade;
import com.example.code_school.entity.Student;
import com.example.code_school.service.ElectiveService;
import com.example.code_school.service.GradeService;
import com.example.code_school.service.StudentService;
import com.example.code_school.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class StudentController {

    @Autowired
    StudentService studentService;
    @Autowired
    GradeService gradeService;
    @Autowired
    ElectiveService electiveService;

    @RequestMapping  ("/index")
    public String index(String sname,Long gid,@RequestParam(value="pageIndex",defaultValue="1")Integer pageIndex, Model model) {

        /*总条数需要知道*/
        Integer studentsCount = studentService.getStudentsCount(sname,gid);
        Integer size =(int) (studentsCount / PageUtil.PAGESIZE);
        if(studentsCount % PageUtil.PAGESIZE != 0){
            size++;
        }

        if(pageIndex>size){
            pageIndex=1;
        }
        List<Student> students = studentService.getStudents(sname,gid,pageIndex);

        model.addAttribute("students", students);


        /*查询班级列表*/
        List<Grade> grades = gradeService.getGrades();
        model.addAttribute("grades", grades);

        /*查询科目列表*/
        List<Elective> electives = electiveService.getElectives();
        model.addAttribute("electives", electives);



        model.addAttribute("size", size);

        model.addAttribute("pageIndex",pageIndex);

        model.addAttribute("sname", sname);
        model.addAttribute("gid", gid);
        return "index";
    }
}
